package com.springboot.chatroom.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
import org.springframework.web.bind.annotation.RestController;

import com.springboot.chatroom.model.User;
import com.springboot.chatroom.service.UserService;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;

/**
 * Created with IntelliJ IDEA.
 * Description :
 * User: jiume
 * Date: 2025-03-21
 * Time: 13:32
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    // 提供两个接口：登录、注册

    @Autowired
    private UserService userService;

    // 登录接口
    @RequestMapping(value = "/login",method = POST)
    public Object login(String username, String password, HttpSession session) {
        // 校验参数是否合法
        if (!StringUtils.hasLength(username)
                || !StringUtils.hasLength(password)) {
            log.info("参数不合法...");
            return new User();
        }
        // 校验参数是否正确
        // 查询数据库信息，校验密码是否正确
        User user = userService.selectByUsername(username);
        // 如果用户不存在或者密码错误返回null
        if (user == null || !user.getPassword().equals(password)) {
            log.info("用户不存在或者密码错误..."+user);
            return new User();
        }
        // 将用户登录信息到会话中
        session.setAttribute("user", user);
        // 将password置为空
        user.setPassword("");
        return user;
    }

    // 注册接口
    @RequestMapping(value = "/register",method = POST)
    public Object register(String username, String password) {
        try {
            Integer result = userService.insertUser(username, password);
        } catch (DuplicateKeyException e) {
            // 程序运行到这里说明用户名重复，插入失败
            log.info("注册失败...username: "+username);
            return new User();
        }
        log.info("注册成功...username: "+username);
        Integer userId = userService.selectByUsername(username).getUserId();
        return new User(userId,username,"");
    }

    // 获取用户信息
    @RequestMapping(value = "/userInfo",method = GET)
    public Object getUserInfo(HttpServletRequest request) {
        // 1、获取请求的会话信息
        HttpSession session = request.getSession(false);
        if (session == null) {
            // 当前请求的会话不存在，即用户并未登录（或登录超时）
            log.info("[getUserInfo] 当前获取不到 session 对象! ");
            return new User();
        }
        // 2、根据会话信息获取具体的用户信息
        User user = (User) session.getAttribute("user");
        if (user == null) {
            // 说明该用户可能并未登录或者说登录超时的情况
            log.info("[getUserInfo] 当前获取不到 user 对象! ");
            return new User();
        }
        // 将user对象进行脱敏操作
        user.setPassword("");
        return user;
    }
}
